package com.yokit.c2c.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yokit.c2c.pojo.Goods;
import com.yokit.c2c.pojo.Shop;
import com.yokit.c2c.pojo.User;
import com.yokit.c2c.response.ResponseObject;
import com.yokit.c2c.service.GoodsService;
import com.yokit.c2c.service.ShopService;
import com.yokit.c2c.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.swing.text.html.Option;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author fengzeng
 * @create 2021/6/13 21:24
 */
@RestController
public class GoodsController {

  @Resource
  private GoodsService goodsService;

  @Resource
  private ShopService shopService;

  /**
   * 获取所有商品列表
   *
   * @return
   */
  @RequestMapping(value = "/goods", method = RequestMethod.GET)
  public ResponseObject getGoodsList() {
    List<Goods> goodsList = goodsService.selectAll();
    return new ResponseObject(200, "获取商品列表成功", goodsList);
  }

  /**
   * 获取商品分类
   *
   * @param catId
   * @return
   */
  @RequestMapping(value = "/goodsCat/{catId}", method = RequestMethod.GET)
  public ResponseObject getGoodsCat(@PathVariable String catId) {
    Integer catIdInteger = Integer.parseInt(catId);
    List<Goods> goodsList = goodsService.selectByCatId(catIdInteger);
    return new ResponseObject(200, "获取商品分类成功", goodsList);
  }

  /**
   * 根据goods id查询商品
   *
   * @param goodsId
   * @return
   */
  @RequestMapping(value = "/goods/{goodsId}", method = RequestMethod.GET)
  public ResponseObject selectByGoodsId(@PathVariable("goodsId") String goodsId) {
    Optional<Goods> goodsOptional = Optional.ofNullable(goodsService.selectByGoodsId(Integer.valueOf(goodsId)));
    if (goodsOptional.isPresent()) {
      return new ResponseObject(200, "获取商品成功", goodsOptional.get());
    }
    return new ResponseObject(400, "获取商品失败");
  }

  /**
   * 精选商品
   *
   * @return
   */
  @RequestMapping(value = "/hotGoods", method = RequestMethod.GET)
  public ResponseObject getHotGoods() {
    List<Goods> goods = goodsService.select5HotGoods();
    return new ResponseObject(200, "获取热门商品成功", goods);
  }

  /**
   * 创建商品
   *
   * @param request
   * @return
   */
  @RequestMapping(value = "/goods", method = RequestMethod.POST)
  public ResponseObject createGoods(@RequestBody Map<String, String> request) {

    Goods goods = new Goods();

    //获取商品分类id
    Integer catId = Integer.valueOf(request.get("catId"));
    goods.setCatId(catId);

    //获取商品名称
    String goodsName = request.get("goodsName");
    goods.setGoodsName(goodsName);

    //获取商品价格
    BigDecimal price = BigDecimal.valueOf(Integer.parseInt(request.get("goodsPrice")));
    goods.setGoodsPrice(price);

    //根据UserId查询店铺信息
    Integer userId = Integer.parseInt(request.get("userId"));
    Shop shop = shopService.selectShopByUserId(userId);

    //设置商品图片
    String goodsPictureUrl = "/image/" + request.get("goodsPicture");
    goods.setGoodsPicture(goodsPictureUrl);

    //设置商铺id
    goods.setShopId(shop.getShopId());


    goodsService.createGoods(goods);

    return new ResponseObject(200, "创建商品成功");

  }

  /**
   * 搜索商品
   *
   * @param goodsName
   * @return
   */
  @RequestMapping(value = "/searchGoods", method = RequestMethod.GET)
  public ResponseObject getSearchGoods(@RequestParam("goodsName") String goodsName) {
    List<Goods> goodsList = goodsService.selectByGoodsName(goodsName);
    return new ResponseObject(200, "查找成功", goodsList);
  }

  @RequestMapping(value = "/goods/user/{userId}", method = RequestMethod.GET)
  public Map getGoodsByUserId(@PathVariable String userId,
                              @RequestParam("pageSize") String pageSize,
                              @RequestParam("pageNum") String pageNum) {
    //获取用户Id
    Integer userIdInt = Integer.parseInt(userId);
    //获取分页参数
    Integer pageSizeInt = Integer.parseInt(pageSize);
    Integer pageNumInt = Integer.parseInt(pageNum);
    //获取用户的商铺信息
    Shop shop = shopService.selectShopByUserId(userIdInt);
    //开启分页
    PageHelper.startPage(pageNumInt, pageSizeInt);
    List<Goods> goodsList = goodsService.selectByShopId(shop.getShopId());
    PageInfo pageInfo = new PageInfo(goodsList);
    Map res = new HashMap();
    res.put("status", 200);
    res.put("msg", "获取商品成功");
    res.put("total", pageInfo.getTotal());
    res.put("data", pageInfo.getList());
    return res;
  }

  /**
   * 编辑商品
   *
   * @param goods
   * @return
   */
  @RequestMapping(value = "/editGoods", method = RequestMethod.PUT)
  public ResponseObject editGoods(@RequestBody Goods goods) {
    goodsService.updateGoods(goods);
    return new ResponseObject(200, "操作成功");
  }

  /**
   * 删除商品
   *
   * @param goodsId
   * @return
   */
  @RequestMapping(value = "/goods/{goodsId}", method = RequestMethod.DELETE)
  public ResponseObject deleteGoodsById(@PathVariable String goodsId) {
    Integer goodsIdInt = Integer.parseInt(goodsId);
    goodsService.deleteGoodsById(goodsIdInt);
    return new ResponseObject(200, "删除商品成功");
  }

}
